package com.junyuan.fxf_pad.activity.fragment;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.junyuan.fxf_pad.R;
import com.junyuan.fxf_pad.activity.MoreMusicActivity;
import com.junyuan.fxf_pad.application.MyApplication;
import com.junyuan.fxf_pad.utils.Consts;

public class MusicFragment extends Fragment implements View.OnClickListener {
    ImageView ivAlbum;
    ImageButton ibPlay, ibPrevious, ibNext;
    boolean isPlaying = false;
    ObjectAnimator animator;
    boolean selectedMusic = false;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_music, null);
        initViews(view);
        setHasOptionsMenu(true);
        return view;

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_more_music, menu);
        super.onCreateOptionsMenu(menu, inflater);
        Log.d("123", "创建菜单");
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent(getContext(), MoreMusicActivity.class);
        startActivityForResult(intent, 0);
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("123", "resultCode:" + resultCode + "");
        if (resultCode == Consts.MUSIC_SELECTED) {
            Log.d("123", "选择了歌曲");
            ibPlay.setSelected(true);
            isPlaying = true;
            if (animator != null) {
                animator.cancel();
            }
            animator = ObjectAnimator.ofFloat(ivAlbum, "rotation", 0f, 360f);
            animator.setInterpolator(new LinearInterpolator());
            animator.setDuration(8000);
            animator.setRepeatCount(-1);
            animator.start();
            selectedMusic = true;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (animator != null && selectedMusic == false) {
            animator.cancel();
            animator = null;
        }
        isPlaying = false;
        selectedMusic = false;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibPlay:
                if (isPlaying == false) {
                    ibPlay.setSelected(true);
                    isPlaying = true;
                    play();
                    if (MyApplication.instance.mBleService != null) {
                        MyApplication.instance.mBleService.writeCharacteristic(MyApplication.instance.mCharacteristic, "C");
                    }
                } else if (isPlaying == true) {
                    ibPlay.setSelected(false);
                    isPlaying = false;
                    play();
                    if (MyApplication.instance.mBleService != null) {
                        MyApplication.instance.mBleService.writeCharacteristic(MyApplication.instance.mCharacteristic, "D");
                    }
                }
                break;
            case R.id.ibPrevious:
                ibPlay.setSelected(true);
                isPlaying = true;
                changeMusic();
                if (MyApplication.instance.mBleService != null) {
                    MyApplication.instance.mBleService.writeCharacteristic(MyApplication.instance.mCharacteristic, "A");
                }
                break;
            case R.id.ibNext:
                ibPlay.setSelected(true);
                isPlaying = true;
                changeMusic();
                if (MyApplication.instance.mBleService != null) {
                    MyApplication.instance.mBleService.writeCharacteristic(MyApplication.instance.mCharacteristic, "B");
                }
                break;
        }
    }

    public void changeMusic() {
        if (animator == null) {
            animator = ObjectAnimator.ofFloat(ivAlbum, "rotation", 0f, 360f);
            animator.setInterpolator(new LinearInterpolator());
            animator.setDuration(8000);
            animator.setRepeatCount(-1);
            animator.start();
        }
        if (!animator.isRunning()) {
            animator.start();
        } else if (animator.isRunning()) {
            animator.cancel();
            animator.start();
        }
    }

    public void play() {
        if (animator == null) {
            animator = ObjectAnimator.ofFloat(ivAlbum, "rotation", 0f, 360f);
            animator.setInterpolator(new LinearInterpolator());
            animator.setDuration(8000);
            animator.setRepeatCount(-1);
        }
        if (!animator.isRunning()) {
            animator.start();
        } else if (animator.isRunning()) {
            if (animator.isPaused()) {
                animator.resume();
            } else if (!animator.isPaused()) {
                animator.pause();
            }
        }

    }

    private void initViews(View view) {
        ivAlbum = (ImageView) view.findViewById(R.id.ivMusicInfo);
        ibPlay = (ImageButton) view.findViewById(R.id.ibPlay);
        ibPrevious = (ImageButton) view.findViewById(R.id.ibPrevious);
        ibNext = (ImageButton) view.findViewById(R.id.ibNext);

        ibPlay.setOnClickListener(this);
        ibPrevious.setOnClickListener(this);
        ibNext.setOnClickListener(this);
    }


}
